#include "../../framework/framework.h"
#include "../inc/xLine.h"
#include "../inc/xDevice.h"
namespace Sky
{
	xLine::xLine()
	{
		Initialise();
	}

	xLine::~xLine()
	{
		Finalise();
	}

	TResult xLine::onCreate( xDxApp* app )
	{
		return XE_OK;
	}

	TResult xLine::onCreate( xDevice* device )
	{
		TResult hr;
		m_Device = device;
		myDxV_Ret_hr_M(D3DXCreateLine(device->getDevice9(),&m_Res),XE_APICALL);
		return XE_OK;
	}
	TResult xLine::onDelete( xDxApp* app )
	{
		SAFE_RELEASE(m_Res);
		return XE_OK;
	}

	TResult xLine::onDelete( xDevice* device )
	{
		SAFE_RELEASE(m_Res);
		return XE_OK;
	}
	TResult xLine::onReset( xDevice* device )
	{
		xAssertA(m_Res&&"m_Res == vNull in onReset()");
		myDxV_Ret_hr_M(m_Res->OnResetDevice(),XE_APICALL);
		return XE_OK;
	}

	TResult xLine::onLost( xDevice* device )
	{
		xAssertA(m_Res&&"m_Res == vNull in onReset()");
		myDxV_Ret_hr_M(m_Res->OnLostDevice(),XE_APICALL);
		return XE_OK;
	}

	TResult xLine::drawLine( D3DXVECTOR2* start,D3DXVECTOR2* end )
	{
		D3DXVECTOR2 vec[2];
		vec[0] = *start;
		vec[1] = *end;
		if(m_Res == vNull){
			myDeV_Ret_M(trySelfCreate())
		}
		D3DRECT rect;
		rect.x1 = start->x;
		rect.y1 = start->y;
		rect.x2 = end->x;
		rect.y2 = end->y;
		m_Res->Draw(vec,2,m_Color);
		
		return XE_OK; 
	}

	TVoid xLine::Initialise()
	{
		m_Color = D3DCOLOR_RGBA(0xff,0xff,0,0xff);
	}

	TVoid xLine::Finalise()
	{
		SAFE_RELEASE(m_Res);
	}

	xLine& xLine::setWidth( TFloat width )
	{
		if(m_Res == vNull){
			myDeV_Ret_hr_M(trySelfCreate(),*this);
		}
		m_Width = width;
		m_Res->SetWidth(width);
		return *this;
	}

	xLine& xLine::setPattern( TUint pattern )
	{
		if(m_Res == vNull){
			myDeV_Ret_hr_M(trySelfCreate(),*this);
		}
		m_Res->SetPattern(pattern);
		return *this;
	}
}